﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccess.SingleSignOn;
using Single_SignOn.Areas.Handheld.Models;
using Single_SignOn.Attributes;
using Single_SignOn.Json;
using Single_SignOn.ResX;
using UserProfile = Single_SignOn.Models.UserProfile;

namespace Single_SignOn.Areas.Handheld.Controllers
{
    public class ChangeSwordController : Controller
    {
        //
        // GET: /Handheld/ChangeSword/

        public AuthenConnection oAuth;

        public ActionResult Index()
        {
            return View("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [JsonFilter(Param = "oChangnPasswordDataObj", RootType = typeof(oChangePassword))]
        public ActionResult Confirm(oChangePassword oChangnPasswordDataObj)
        {
            JsonResult js = new JsonResult();

            oAuth = new AuthenConnection();

            if (oAuth.pullUserProfile(oChangnPasswordDataObj.Username).Rows.Count <= 0)
            {
                js = Json(new
                {
                    redirectToUrl = Url.Action("Index"
                        , "Login"
                        , ""
                        , "http")
                    ,
                    exception = ResourceTools.GetResource("Resource", "CheckUsername")
                });
            }
            else
            {
                if (oAuth.pullUserProfile(oChangnPasswordDataObj.Username, oChangnPasswordDataObj.OldPassword)
                    .Rows.Count <= 0)
                {
                    js = Json(new
                    {
                        redirectToUrl = Url.Action("Index"
                            , "Login"
                            , ""
                            , "http")
                        ,
                        exception = ResourceTools.GetResource("Resource", "CheckOldPassword")
                    });
                }
                else
                {
                    if (oAuth.upChangePassword(oChangnPasswordDataObj.Username, th.co.homepro.Utility.zEncrypt(oChangnPasswordDataObj.NewPassword, WebConfig.Encryptkey, WebConfig.EncryptValue)))
                    {
                        js = Json(new
                        {
                            redirectToUrl = Url.Action("Index"
                                , "Login"
                                , ""
                                , "http")
                             ,
                            exception = ""
                        });
                    }
                }
            }
           
            return js;
        }

    }
}
